<template>
  <!-- 智能催收-邮件催收 -->
  <section class="page mail-collection">
    <data-form :model="model" @onSearch="refreshData" :page="pageService" @handleResetForm="handleResetForm">
      <template slot="inputs">
        <el-form-item label="订单编号" prop="caseNumber">
          <el-input v-model.trim="model.caseNumber"></el-input>
        </el-form-item>
        <el-form-item label="客户姓名" prop="personalName">
          <el-input v-model.trim="model.personalName"></el-input>
        </el-form-item>
        <el-form-item label="当前催收员" prop="currentCollectorRealName">
          <el-input v-model.trim="model.currentCollectorRealName"></el-input>
        </el-form-item>
        <el-form-item label="逾期期数" prop="overduePeriod">
          <el-select v-model="model.overduePeriod" placeholder="请选择" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('OverduePeriodStatus')" :key="index" :label="name" :value="code.replace(/M|\+/, '')"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="案件金额" prop="caseMoney">
          <number-range v-model="model.caseMoney" :isInteger="true"></number-range>
        </el-form-item>
      </template>
    </data-form>

    <data-box :data="dataSet" :selectionList.sync="selectionList" @onPageChange="refreshData" :sort="sortService" :page="pageService">
      <template slot="buttons">
        <el-button @click="batchMail" v-auth="2211">电子邮件群发</el-button>
      </template>
      <template slot="columns">
        <el-table-column prop="caseNumber" label="订单编号" :min-width="$helper.getColumnWidth(6)">
        </el-table-column>
        <el-table-column prop="personalName" label="客户姓名" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="mobileNo" label="手机号" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="caseMoney" label="案件金额(元)" :min-width="$helper.getColumnWidth(4)"></el-table-column>
        <el-table-column prop="overdueAmount" label="欠款金额(元)" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="overduePeriod" label="逾期期数" :min-width="$helper.getColumnWidth(3)" :formatter="row => $common.isNotValue(row.overduePeriod) ? '':`M${row.overduePeriod}`">
        </el-table-column>
        <el-table-column prop="overdueDays" label="逾期天数" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="currentCollector" label="永久催收员" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="followinTime" label="案件流入时间" :formatter="(row) => $filter.dateTimeFormat(row.followinTime)" :min-width="$helper.getColumnWidth(5)">
        </el-table-column>
      </template>
    </data-box>

    <!-- 批量发送邮件 -->
    <el-dialog :visible.sync="dialog.mailSendList" :show-close="false" :close-on-click-modal="false" :close-on-press-escape="false" @open="$nextTick(() => $refs['mail-send-list'].refreshData())">
      <mail-send-list ref="mail-send-list" :selectionList="selectionList" @close="refreshData(), dialog.mailSendList = false"></mail-send-list>
    </el-dialog>
  </section>
</template>

<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import { Auth, Layout, Dependencies } from "~/core/decorator";
import { PageService } from "~/utils/page.service";
import { SortService } from "~/utils/sort.service";
import DataForm from "~/components/common/data-form.vue";
import DataBox from "~/components/common/data-box.vue";
import { Getter, Mutation, namespace } from "vuex-class";
import { SmartCollectionService } from "~/services/domain-service/smart-collection.service";
import MailSendList from "~/components/smart-collection/mail-collection/mail-send-list.vue";
import NumberRange from '~/components/common/number-range.vue';

@Auth(2202)
@Layout("workspace")
@Component({
  components: {
    MailSendList,
    NumberRange,
  }
})
export default class MailCollection extends Vue {
  @Dependencies(PageService)
  private pageService: PageService
  @Dependencies(SortService)
  private sortService: SortService
  @Dependencies(SmartCollectionService)
  private smartCollectionService: SmartCollectionService

  private dataSet: any[] = []
  private selectionList: any[] = []

  private dialog: any = {
    mailSendList: false
  }

  private model: any = {
    caseNumber: '',
    personalName: '',
    currentCollectorRealName: '',
    overduePeriod: '',
    caseMoney: { min: '', max: '' }
  }

  private batchMail() {
    if (this.selectionList.length === 0) {
      this.$message.info('请先选择案件')
      return
    }
    this.dialog.mailSendList = true
  }

  private refreshData() {
    this.smartCollectionService.getAllCollectionCaseInfo(this.model, this.pageService, this.sortService)
      .subscribe((data) => {
        this.dataSet = data
        this.selectionList = []
      });
  }

  /**
   * 重置
   */
  private handleResetForm() {
    this.model.caseMoney = { min: '', max: '' }
  }

  private mounted() {
    this.refreshData()
  }
}
</script>

<style lang="less" scoped>
</style>
